package com.baicun.wish.spiders;

import com.baicun.wish.sink.SinkToCsv;
import com.baicun.wish.sink.SinkToCsv2;
import com.baicun.wish.utils.driver.WebDriverUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * @author baicun
 * @description 负责爬取 https://gkcx.eol.cn/school/(30-3560) 大学数据
 */
public class CollegeSpider {

    public static void getCollege (int num) throws Exception {


        ChromeDriver chromeDriver = WebDriverUtil.openChrome();
        chromeDriver.get("https://gkcx.eol.cn/school/"+num);

        //隐式等待
        chromeDriver.manage().timeouts().implicitlyWait(500, TimeUnit.MILLISECONDS);

        //数据获取

        /*
            **college**

            | --------------------- | ------------ | --------------- |
            | college_name          | varchar(50)  | 大学名字        |
            | avatar                | vachar(255)  | 头像            |
            | province              | varchar(50)  | 省              |
            | region                | varchar(50)  | 市              |
            | popularity_value      | int          | 人气            |
            | start_type            | varchar(50)  | 办学类型        |
            | college_type          | varchar(50)  | 学校类型        |
            | college_tag(list)     | vachar(255)  | 学校标签（list) |
            | official_url(list)    | vachar(255)  | 官方网址        |
            | official_phone(list)  | vachar(255)  | 官方电话        |
            | official_email（list) | varchar(255) | 官方邮箱        |
            | found_year            | int          | 建校年份        |
            * attach                varchar(50)      隶属
            | area                  | double       | 学校面积        |
            | address               | vachar(255)  | 学校地址        |
            | data_url              | vachar(255)  | 爬取地址        |

         */

        String collegeName = ElementUtil.isExist(chromeDriver,By.className("line1-schoolName")) ? chromeDriver.findElement(By.className("line1-schoolName")).getText() : "";

        String avatar = ElementUtil.isExist(chromeDriver, By.className("schoolLogo")) ? chromeDriver.findElement(By.className("schoolLogo")).findElement(By.tagName("img")).getAttribute("src") : "";

        String region = ElementUtil.isExist(chromeDriver,By.className("line1-province")) ? chromeDriver.findElement(By.className("line1-province")).getText() : "";

        String province = region.contains("内蒙古") ? region.contains("黑龙江")? "黑龙江":"内蒙古" : region.contains("黑龙江")? "黑龙江" : region.substring(0,2);

        String popularityValue = "";
        try {
            popularityValue = ElementUtil.isExist(chromeDriver,By.className("line1-rank")) ? chromeDriver.findElement(By.className("line1-rank")).getText().split(":")[1] : "";
        }catch (Exception e){
//            e.printStackTrace();
            System.out.println("popularityValue没有数据！");
        }

        String startType = ElementUtil.isExist(chromeDriver,By.className("line2_item")) ? chromeDriver.findElement(By.className("line2_item")).getText() : "";


        String collegeType = ElementUtil.isExist(chromeDriver,By.className("line2_item")) ? chromeDriver.findElements(By.className("line2_item")).get(1).getText() : "";

        String collegeTag = "";
        if(ElementUtil.isExist(chromeDriver,By.className("line2"))){
            List<WebElement> elements = chromeDriver.findElements(By.className("line2_item"));
            for (int i = 2; i < elements.size() ; i++) {
                collegeTag += elements.get(i).getText();
                collegeTag += "|";
            }
            collegeTag=collegeTag.substring(0,collegeTag.length()-1);
        }

        String officialUrl = ElementUtil.isExist(chromeDriver,By.className("line3_item")) ? chromeDriver.findElements(By.className("line3_item")).get(0).findElement(By.tagName("a")).getText() : "";

        String officialPhone = "";
        try {
            officialPhone = ElementUtil.isExist(chromeDriver,By.className("line3_item")) ? chromeDriver.findElements(By.className("line3_item")).get(1).getText().split("：")[1].replace(",","|") : "";
        }catch (Exception e){
//            e.printStackTrace();
            System.out.println("officialPhone没有数据！");
        }

        String officialEmail = "";
        try {
            officialEmail = ElementUtil.isExist(chromeDriver,By.className("line3_item")) ? chromeDriver.findElements(By.className("line3_item")).get(2).getText().split("：")[1].replace(",","|") : "";
        }catch (Exception e){
//            e.printStackTrace();
            System.out.println("officialEmail没有数据！");
        }

        String foundYear = "";
        try {
            foundYear = ElementUtil.isExist(chromeDriver,By.className("ellipsis")) ? chromeDriver.findElements(By.className("ellipsis")).get(0).getText().split("：")[1] : "";
        }catch (Exception e){
//            e.printStackTrace();
            System.out.println("foundYear没有数据！");
        }

        String attach = "";
        try {
            attach = ElementUtil.isExist(chromeDriver,By.className("ellipsis")) ? chromeDriver.findElements(By.className("ellipsis")).get(1).getText().split("：")[1] : "";
        }catch (Exception e){
//            e.printStackTrace();
            System.out.println("attach没有数据！");
        }

        String area = "";
        try {
            area = ElementUtil.isExist(chromeDriver,By.className("ellipsis")) ? chromeDriver.findElements(By.className("ellipsis")).get(2).getText().split("：")[1] : "";
        }catch (Exception e){
//            e.printStackTrace();
            System.out.println("area没有数据！");
        }

        String address = ElementUtil.isExist(chromeDriver,By.className("ellipsis")) ? chromeDriver.findElements(By.className("ellipsis")).get(3).getText() : "";

        String dataUrl = "https://gkcx.eol.cn/school/"+num;

        System.out.println(num+":"+collegeName+","+avatar+","+province+","+region+","+popularityValue+","+startType+","+collegeType+","+collegeTag
                +","+officialUrl+","+officialPhone+","+officialEmail+","+foundYear+","+attach+","+area+","+address+","+dataUrl);

        SinkToCsv.writeContent(new String[]{collegeName,avatar,province,region,popularityValue,startType,collegeType,collegeTag,
                officialUrl,officialPhone,officialEmail,foundYear,attach,area,address,dataUrl});

        /*
            **college_basic**

            | -------------- | ------------ | ---------- |
            | college_name   | varchar(50)  | 大学名字   |
            | study_index    | int          | 学习指数   |
            | live_index     | int          | 生活指数   |
            | work_index     | int          | 就业指数   |
            | complex_index  | int          | 综合指数   |
            | introduce_text | varchar(500) | 学校简介   |
            | boy_scale      | varchar(50)  | 男比例     |
            | girl_scale     | varchar(50)  | 女比例     |
            | work_scale     | varchar(50)  | 就业率     |
            | further_scale  | varchar(50)  | 国内考研率 |
            | abroad_scale   | varchar(50)  | 出国留学率 |
            | pictures(list) | longtext     | 学校图片   |
         */
        String studyIndex = "";
        String liveIndex = "";
        String workIndex = "";
        String complexIndex = "";
        String introduceText = "";
        String boyScale = "";
        String girlScale = "";
        String workScale = "";
        String furtherScale = "";
        String abroadScale = "";
        try{
            studyIndex = ElementUtil.isExist(chromeDriver,By.className("circle_item")) ? chromeDriver.findElements(By.className("circle_item")).get(0).findElement(By.tagName("span")).getText() : "";
            liveIndex = ElementUtil.isExist(chromeDriver,By.className("circle_item")) ? chromeDriver.findElements(By.className("circle_item")).get(1).findElement(By.tagName("span")).getText() : "";
            workIndex = ElementUtil.isExist(chromeDriver,By.className("circle_item")) ? chromeDriver.findElements(By.className("circle_item")).get(2).findElement(By.tagName("span")).getText() : "";
            complexIndex = ElementUtil.isExist(chromeDriver,By.className("circle_item")) ? chromeDriver.findElements(By.className("circle_item")).get(3).findElement(By.tagName("span")).getText() : "";
            introduceText = ElementUtil.isExist(chromeDriver,By.className("school_introduce_text_item")) ? chromeDriver.findElement(By.className("school_introduce_text_item")).getText() : "";
            boyScale = ElementUtil.isExist(chromeDriver,By.className("rate_num")) ? chromeDriver.findElement(By.className("rate_num")).findElements(By.tagName("span")).get(0).getText() : "";
            girlScale = ElementUtil.isExist(chromeDriver,By.className("circle_item")) ? chromeDriver.findElement(By.className("rate_num")).findElements(By.tagName("span")).get(1).getText() : "";
            workScale = ElementUtil.isExist(chromeDriver,By.className("job_circle")) ? chromeDriver.findElements(By.className("job_circle")).get(0).getText() : "";
            furtherScale = ElementUtil.isExist(chromeDriver,By.className("circle_item")) ? chromeDriver.findElements(By.className("job_circle")).get(1).getText() : "";
            abroadScale = ElementUtil.isExist(chromeDriver,By.className("circle_item")) ? chromeDriver.findElements(By.className("job_circle")).get(2).getText() : "";

        }catch (Exception e){
            System.out.println("找不到元素！");
        }

        String pictures = "";
        try{
            if(ElementUtil.isExist(chromeDriver,By.className("swiper-wrapper")) ){
                List<WebElement> imgs = chromeDriver.findElement(By.className("swiper-wrapper")).findElements(By.tagName("img"));
                for (WebElement e : imgs) {
                    pictures += e.getAttribute("src");
                    pictures += "|";
                }
                pictures = pictures.substring(0,pictures.length()-1);
            }
        }catch (Exception e){
            System.out.println("找不到图片！");
        }


        System.out.println(studyIndex+","+liveIndex+","+workIndex+","+complexIndex+","+introduceText+","+boyScale+","
                +girlScale+","+workScale+","+furtherScale+","+abroadScale+","+pictures);

        SinkToCsv2.writeContent(new String[]{collegeName,studyIndex,liveIndex,workIndex,complexIndex,introduceText,boyScale,girlScale,workScale,furtherScale,abroadScale,pictures});


    }


}
